
#include "utils/vectorutils.h"
#include <iostream>
#include <cassert>

using namespace std;

void testEquals() {
    vector<byte> v1(4,0);
    vector<byte> v2(5,0);
    assert(!VectorUtils::equals(v1, v2));
    v2 = vector<byte>(4,0);
    assert(VectorUtils::equals(v1, v2));
    
    for(int i = 0; i < v1.size(); i++){
        v1[i] = i;
    }
    assert(!VectorUtils::equals(v1, v2));
    cout << "OK : equals" << endl;

}


void testSacadores() {
    vector<byte> v1(2, 0);
    VectorUtils::sacarCerosFinal(v1);
    vector<byte> v2(0,0);
    assert(VectorUtils::equals(v1, v2));
    v1 = vector<byte>(2,0);
    VectorUtils::sacarCerosPrincipio(v1);
    assert(VectorUtils::equals(v1, v2));
    v2 = vector<byte>(1,0);
    v2[0] = 1;
    v1 = vector<byte>(10,0);
    v1[0] = 1;
    VectorUtils::sacarCerosFinal(v1);
    assert(VectorUtils::equals(v1, v2));
    v1 = vector<byte>(10,0);
    v1[9] = 1;
    VectorUtils::sacarCerosPrincipio(v1);
    assert(VectorUtils::equals(v1, v2));

    cout << "OK : sacadores" << endl;
}

void testSumaResta(){
    vector<byte> v1(3,0);
    vector<byte> v2(3,0);
    v1[0] = 1;
    v2[0] = 2;
    v1[1] = 2;
    v2[1] = 1;
    v1[2] = 3;
    v2[2] = 0;
    vector<byte> res = vector<byte>(3,0);
    res[0] = 3; res[1] = 3; res[2] = 3;
    VectorUtils::sumaDirecta(v1, v2);
    assert(VectorUtils::equals(res, v1));
    VectorUtils::restaDirecta(res, v1);
    assert(VectorUtils::equals(res, vector<byte>(3,0)));
    v2[0] = 255;
    v2[1] = 255;
    v2[2] = 255;
    v1 = vector<byte>(3,0);
    v1[2] = 1;
    res = vector<byte>(4,0);
    res[0] = 1;
    VectorUtils::sumaDirecta(v1, v2);
    assert(VectorUtils::equals(res, v1));
    cout << "OK : Suma y Resta" << endl;
}

void testReplace() {
    vector<byte> v1,v2, res;
    v1 = vector<byte>(4,0);
    res = vector<byte>(4,0);
    v2 = vector<byte>(2,0);
    res[2] = 34;
    res[3] = 45;
    v1[2] = 3;
    v1[3] = 2;
    v2[0] = 34;
    v2[1] = 45;
    VectorUtils::reemplazar(v1, 2, v2);
    assert(VectorUtils::equals(v1,res));
    cout << "OK : Reemplazar" << endl;
}

void testMultiplicacionDirecta() {
    vector<byte> v1,v2, res, expected;
    v1 = vector<byte>(2,0);
    v2 = vector<byte>(2,0);
    expected = vector<byte>(4,0);
    v1[0] = 1;
    v2[1] = 67;
    res = VectorUtils::multiplicacionDirecta(v1, v2);
    expected[2] = 67;
    assert(VectorUtils::equals(expected,res));

    v1[0] = 1;
    v1[1] = 129;
    v2[1] = 2;
    res = VectorUtils::multiplicacionDirecta(v1, v2);
    expected[3] = 2;
    expected[2] = 3;
    assert(VectorUtils::equals(expected,res));

    v1[0] = 255;
    v1[1] = 255;
    v2[0] = 255;
    v2[1] = 255;
    res = VectorUtils::multiplicacionDirecta(v1, v2);
    expected[0] = 255;
    expected[1] = 254;
    expected[2] = 0;
    expected[3] = 1;
    assert(VectorUtils::equals(expected,res));

    cout << "OK : Multiplicacion" << endl;
}

void testCortar() {
    vector<byte> v1,v2, res, expected;
    v1 = vector<byte>(5,0);
    int i = 0;
    while(i < 5) {
        v1[i] = i;
        i++;
    }
    VectorUtils::cortarMaxPrecision(v1, 3);
    assert(v1.size() ==  3);
    i = 0;
    while(i < 3){
        assert(v1[i] == i);
        i++;
    }
    cout << "OK : Corte" << endl;
}


void testvectorutils() {
    cout << endl << "VECTOR UTILS TESTS" << endl;
    testEquals();
    testSacadores();
    testSumaResta();
    testReplace();
    testMultiplicacionDirecta();
    testCortar();
}
